/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "workerclearcache.h"
#include "configuration.h"

WorkerClearCache::WorkerClearCache(const QString& image_id,
                                   const QString& title) :
    WorkerProcess(1, title)
{
    path = configuration->defaultPath() + "/map_storage/" + image_id;
}

bool WorkerClearCache::do_stuff()
{
    int n_files = 0;
    int max_files = 0;

    QDir folder(path);
    QStringList files = folder.entryList();
    files.removeOne( "." );
    files.removeOne( ".." );

    max_files = files.count();
    setMinMaxProgress( max_files );

    QString str = tr("Deleted chunks: %1 / ") + QString("%1").arg(max_files);

    foreach (const QString& file, files )
    {
        QFile::remove( path + "/" + file );
        n_files++;
        updateText( 0, str.arg( n_files ) );
        updateProgress(n_files);
        if ( please_terminate )
            return false;
    }
    folder.rmdir(".");
    return true;
}

